﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OracleClient;
namespace BenefitHelper.Data.ServerData
{
    /// <summary>
    /// 结算资金
    /// </summary>
    public class T_SettlementMoney
    {
        /// <summary>
        /// 编号
        /// </summary>
        public int Id { get; set; }
        /// <summary>
        /// 账户
        /// </summary>
        public int AccountId { get; set; }
        /// <summary>
        /// 日期时间
        /// </summary>
        public double Datetime { get; set; }
        /// <summary>
        /// 多仓持仓
        /// </summary>
        public int LongPosition { get; set; }
        /// <summary>
        /// 空仓持仓
        /// </summary>
        public int ShortPosition { get; set; }
        /// <summary>
        /// 期初结存
        /// </summary>
        public double PresetTlePosit { get; set; }
        /// <summary>
        /// 期末结存
        /// </summary>
        public double SettlePosit { get; set; }
        /// <summary>
        /// 冻结保证金
        /// </summary>
        public double FrozenMargin { get; set; }
        /// <summary>
        /// 冻结手续费
        /// </summary>
        public double FrozenCommission { get; set; }
        /// <summary>
        /// 入金金额
        /// </summary>
        public double Deposit { get; set; }
        /// <summary>
        /// 出金金额
        /// </summary>
        public double WithDraw { get; set; }
        /// <summary>
        /// 交割保证金
        /// </summary>
        public double DeliveryMargin { get; set; }
        /// <summary>
        /// 交割手续费
        /// </summary>
        public double DeliveryCommission { get; set; }
        /// <summary>
        /// 追加保证金
        /// </summary>
        public double AppendMargin { get; set; }
        /// <summary>
        /// 基础保证金
        /// </summary>
        public double BaseMargin { get; set; }
        /// <summary>
        /// 质押金额
        /// </summary>
        public double Mortgage { get; set; }
        /// <summary>
        /// 上次质押金额
        /// </summary>
        public double Premortgage { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Commission { get; set; }
        /// <summary>
        /// 可用资金
        /// </summary>
        public double AvailableMoney { get; set; }
        /// <summary>
        /// 可取资金
        /// </summary>
        public double WithDrawQuota { get; set; }
        /// <summary>
        /// 动态权益
        /// </summary>
        public double DynamicMoney { get; set; }
        /// <summary>
        /// 占用保证金
        /// </summary>
        public double Margin { get; set; }
        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double CloseProfit { get; set; }
        /// <summary>
        /// 持仓盈亏
        /// </summary>
        public double PositionProfit { get; set; }
        /// <summary>
        /// 风险度
        /// </summary>
        public double RiskRate { get; set; }
        /// <summary>
        /// 结算编号
        /// </summary>
        public int SettleMentID { get; set; }
        /// <summary>
        /// 交易日
        /// </summary>
        public string TradingDay { get; set; }



        /// <summary>
        /// 查询结算后的资金情况
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        private System.Collections.Generic.List<T_SettlementMoney> GetInfo(string pdate)
        {
            List<T_SettlementMoney> ios = new List<T_SettlementMoney>();
            T_Settle_History tsh = new T_Settle_History();
            int settlementid = tsh.GetDayMaxSettlementInfoId(pdate);
            if (settlementid != -1)
            {
                ios = GetSettleMoneyInfo(settlementid, pdate);
            }
            return ios;
        }

        /// <summary>
        /// 查询某天最大结算编号的资金记录
        /// </summary>
        /// <param name="settlementid">结算编号</param>
        /// <param name="pdate">日期</param>
        /// <returns></returns>
        private System.Collections.Generic.List<T_SettlementMoney> GetSettleMoneyInfo(int settlementid, string pdate)
        {
            System.Collections.Generic.List<T_SettlementMoney> tsm = new System.Collections.Generic.List<T_SettlementMoney>();
            string sql = "select * from T_SettlementMoney where SettlementId='" + settlementid + "' and tradingday='" + pdate + "'";
            OracleDataReader sdr = Common.ADO.Tools.OracleHelper.ExecuteReader(sql);
            while (sdr.Read())
            {
                T_SettlementMoney s = new T_SettlementMoney();
                s.AccountId = Convert.ToInt32(sdr["accountid"]);
                s.AppendMargin = Convert.ToDouble(sdr["appendmargin"]);
                s.AvailableMoney = Convert.ToDouble(sdr["AvailableMoney"]);
                s.BaseMargin = Convert.ToDouble(sdr["BaseMargin"]);
                s.CloseProfit = Convert.ToDouble(sdr["CloseProfit"]);
                s.Commission = Convert.ToDouble(sdr["Commission"]);
                s.Datetime = Convert.ToDouble(sdr["Datetime"]);
                s.DeliveryCommission = Convert.ToDouble(sdr["DeliveryCommission"]);
                s.DeliveryMargin = Convert.ToDouble(sdr["DeliveryMargin"]);
                s.Deposit = Convert.ToDouble(sdr["Deposit"]);
                s.DynamicMoney = Convert.ToDouble(sdr["DynamicMoney"]);
                s.FrozenCommission = Convert.ToDouble(sdr["FrozenCommission"]);
                s.FrozenMargin = Convert.ToDouble(sdr["FrozenMargin"]);
                s.Id = Convert.ToInt32(sdr["Id"]);
                s.LongPosition = Convert.ToInt32(sdr["LongPosition"]);
                s.Margin = Convert.ToDouble(sdr["Margin"]);
                s.Mortgage = Convert.ToDouble(sdr["Mortgage"]);
                s.PositionProfit = Convert.ToDouble(sdr["PositionProfit"]);
                s.Premortgage = Convert.ToDouble(sdr["Premortgage"]);
                s.PresetTlePosit = Convert.ToDouble(sdr["PresetTlePosit"]);
                s.RiskRate = Convert.ToDouble(sdr["RiskRate"]);
                s.SettlePosit = Convert.ToDouble(sdr["SettlePosit"]);
                s.ShortPosition = Convert.ToInt32(sdr["ShortPosition"]);
                s.WithDraw = Convert.ToDouble(sdr["WithDraw"]);
                s.WithDrawQuota = Convert.ToDouble(sdr["WithDrawQuota"]);
                s.SettleMentID = Convert.ToInt32(sdr["SettleMentID"]);
                s.TradingDay = sdr["TradingDay"].ToString();
                tsm.Add(s);
            }
            sdr.Close();
            return tsm;
        }

        /// <summary>
        /// 每天都需要执行的把Server中的数据导入到分析系统中的方法
        /// </summary>
        /// <param name="pdate">日期</param>
        public void CopyServerSettlementMoneyDataIntoBenefit(string pdate, DB.DBManager db)
        {
            Data.ServerData.T_SettlementMoney h = new Data.ServerData.T_SettlementMoney();
            List<Data.ServerData.T_SettlementMoney> tsm = h.GetInfo(pdate);
            foreach (Data.ServerData.T_SettlementMoney ha in tsm)
            {
                db.SettlementMoneys.Add(ha);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 删除某日的所有持仓明细记录
        /// </summary>
        /// <param name="pdate"></param>
        public void DeleteDaySettlementMoneyFromBenefit(string pdate, DB.DBManager db)
        {
            var historys = from t in db.SettlementMoneys where t.TradingDay.Equals(pdate) select t;
            foreach (Data.ServerData.T_SettlementMoney ha in historys)
            {
                db.SettlementMoneys.Remove(ha);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 得到某日的某账号的结算信息
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public T_SettlementMoney GetSettlementMoney(int accountid, string pdate, DB.DBManager db)
        {
            T_SettlementMoney tsm = new T_SettlementMoney();
            var query = from t in db.SettlementMoneys where t.AccountId == accountid && t.TradingDay.Equals(pdate) select t;
            if (query.Count() > 0)
            {
                tsm = query.First();
            }
            return tsm;
        }
        /// <summary>
        /// 获取收益的涨幅
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="pdate"></param>
        /// <returns></returns>
        public double GetRaiseRate(int accountid, string pdate, DB.DBManager db)
        {
            double raiseRate = 0;
            var query = from t in db.TradeHistorys orderby t.Id descending select t;
            if (query.Count() > 1)
            {
                Sys.TradeHistory th = query.ToList()[1];
                string yesterday = th.GetPdateFromSettlementId(th.Id, db);
                var _query = from t in db.SettlementMoneys where t.AccountId == accountid && t.TradingDay.Equals(yesterday) select t;
                if (_query.Count() > 0)
                {
                    double yesterdayValue = _query.First().CloseProfit - _query.First().Commission;
                    var _query2 = from t in db.SettlementMoneys where t.AccountId == accountid && t.TradingDay.Equals(pdate) select t;
                    double todayValue = _query2.First().CloseProfit - _query2.First().Commission;
                    if (yesterdayValue != 0)
                    {
                        raiseRate = (todayValue - yesterdayValue) / yesterdayValue;
                    }
                    else
                    {
                        raiseRate = todayValue;
                    }
                }
            }
            return Math.Round(raiseRate, 2);
        }
    }
}